package com.xnyzc.lhy.order.service.impl.change;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.order.entity.sms.OaSmsCodeRecord;
import com.xnyzc.lhy.order.entity.user.OaCUser;
import com.xnyzc.lhy.order.entity.user.change.OaChangePhoneRecord;
import com.xnyzc.lhy.order.mapper.sms.OaSmsCodeRecordMapper;
import com.xnyzc.lhy.order.mapper.user.OaCUserMapper;
import com.xnyzc.lhy.order.mapper.user.change.OaChangePhoneRecordMapper;
import com.xnyzc.lhy.order.service.user.change.ChangeMobileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
 * <p>
 *  更换手机号
 * </p>
 *
 * @author wangshuo
 * @since 2019-07-23
 */
@Service
public class ChangeMobileServiceImpl implements ChangeMobileService {

    @Autowired
    private OaChangePhoneRecordMapper oaChangePhoneRecordMapper;

    @Autowired
    private OaCUserMapper oaCUserMapper;

    @Autowired
    private OaSmsCodeRecordMapper oaSmsCodeRecordMapper;


    /**
     * 更换手机号
     * @param token 用户token
     * @param newMobile 新手机号
     * @param verificationCode 验证码
     * @return
     */
    @Override
    public Rv getUserChangeMobile(String token, String newMobile, String verificationCode) {

        //必填验证
        if(StringUtils.isBlank(token)||StringUtils.isBlank(newMobile)||StringUtils.isBlank(verificationCode)){
            throw PangException.create(EErrorCode.noData);
        }
        //查询用户信息
        OaCUser oaCUser=getUserByToken(token);
        if(oaCUser==null){
            throw PangException.create(EErrorCode.noData);
        }
        //验证验证码
        OaSmsCodeRecord codeRecords=getSmsCodeRecord(newMobile,verificationCode);
        if(codeRecords==null){
            throw PangException.create(EErrorCode.noData);
        }
        Date date=new Date();
        codeRecords.setIsUse(1);
        codeRecords.setUpdateTime(date);
        codeRecords.setUpdateUser(oaCUser.getUserId());
        oaSmsCodeRecordMapper.updateById(codeRecords);

       //更换手机号记录表
        OaChangePhoneRecord record=new OaChangePhoneRecord();
        record.setOldPhone(oaCUser.getUserPhone());
        record.setNewPhone(newMobile);
        record.setUserType(0);
        record.setCreateTime(new Date());
        record.setCreateUser(oaCUser.getUserId());
        record.setUpdateTime(new Date());
        record.setUpdateUser(oaCUser.getUserId());
        oaChangePhoneRecordMapper.insert(record);
        //修改用户表手机号
        oaCUser.setUserPhone(newMobile);
        oaCUser.setUpdateTime(date);
        oaCUser.setUpdateUser(oaCUser.getUserId());
        oaCUserMapper.updateById(oaCUser);

        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 通过token查询用户信息
     * @param token
     * @return
     */
    private OaCUser getUserByToken(String token){
        QueryWrapper<OaCUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaCUser.USER_TOKEN,token);
        OaCUser oaCUser = oaCUserMapper.selectOne(queryWrapper);
        return oaCUser;
    }

    /**
     * 验证短信
     * @param newMobile
     * @return
     */
    private OaSmsCodeRecord getSmsCodeRecord(String newMobile,String verificationCode){
        QueryWrapper<OaSmsCodeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSmsCodeRecord.CODE,verificationCode);
        queryWrapper.eq(OaSmsCodeRecord.CODE_TYPE,3);
        queryWrapper.eq(OaSmsCodeRecord.PHONE,newMobile);
        queryWrapper.eq(OaSmsCodeRecord.DELETED,0);
        queryWrapper.eq(OaSmsCodeRecord.USER_TYPE,0);
        queryWrapper.eq(OaSmsCodeRecord.IS_USE,0);
        OaSmsCodeRecord oaSmsCodeRecord = oaSmsCodeRecordMapper.selectOne(queryWrapper);
        return oaSmsCodeRecord;
    }

}
